<template>
  <div>
    <el-table
      :data="tableData"
      border
      style="width: 100%">
      <el-table-column
        fixed
        prop="sid"
        label="学号">
      </el-table-column>
      <el-table-column
        prop="college"
        label="学院">
      </el-table-column>
      <el-table-column
        prop="name"
        label="姓名">
      </el-table-column>
      <el-table-column
        prop="username"
        label="用户名">
      </el-table-column>
<!--      <el-table-column-->
<!--        prop="state"-->
<!--        label="状态">-->
<!--        <template slot-scope="scope">-->
<!--          &lt;!&ndash;            $event是当前的状态，scope.row是该行的对象，scope.$index是该行的序号&ndash;&gt;-->
<!--          <el-switch-->
<!--            v-model="scope.row.state"-->
<!--            active-color="#13ce66"-->
<!--            inactive-color="#ff4949"-->
<!--            :active-value="1"-->
<!--            :inactive-value="0"-->
<!--            @change="change($event,scope.row)">-->
<!--          </el-switch>-->
<!--        </template>-->
<!--      </el-table-column>-->


      <el-table-column
        fixed="right"
        label="操作"
        align="center"
        width="250">
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row)" type="primary" icon="el-icon-edit-outline" size="mini">修改</el-button>
          <el-button @click="deleteStudent(scope.row)" type="danger" icon="el-icon-delete" size="mini">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      background
      layout="prev, pager, next"
      page-size="5"
      :total="total"
      @current-change="page">
    </el-pagination>

    <el-form>
      <el-form-item>
        <el-button type="primary" @click="daka()">发布</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  methods: {
    daka(){
      let _this = this;
      this.$confirm('发布打卡?', '打卡', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        axios.get('http://localhost:8000/std/release').then(function (resp){
          console.log(resp);
          if(resp.data == "success"){
            _this.$message({
              type: 'success',
              message: '发布成功!'
            });
          }else{
            _this.$alert('今天已经发布过打卡了！', '发布失败', {
              confirmButtonText: '确定',
            });
          }
        })
      }).catch(() => {

      });
    },
    deleteStudent(row){
      let _this = this;
      this.$confirm('确认删除该学生?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        axios.get('http://localhost:8000/std/deleteStudent/'+ row.sid +'').then(function (resp){
          if(resp.data == "success"){
            _this.$message({
              type: 'success',
              message: '删除成功!'
            });
          }
        })
      }).catch(() => {
        _this.$message({
          type: 'info',
          message: '取消删除'
        });
      });

    },
    back(){
      this.$router.back();
    },
    change(status,row){
      console.log(row);
      let i = status;
      if(i==1){
        i=0
      }else{
        i=1
      }
      let s;
      if(status==0){
        s = "是否禁用该用户？";
      }else{
        s = "是否启用该用户？";
      }
      let _this = this;
      this.$confirm(s, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        axios.post('http://localhost:8000/std/updateState',row).then(function (resp){
          if(resp.data == "success"){
            _this.$message("添加成功");
          }
        })
      }).catch(() => {
        row.state = i
      });
    },
    handleClick(row) {
      console.log(row);
      let arr = JSON.stringify(row);
      this.$router.push({
        path: '/updateStudent',
        query:{
          row: encodeURIComponent(arr)
        }
      })
    },
    page(currentPage){
      const _this = this;
      axios.get('http://localhost:8000/std/findAll/'+ currentPage +'/5').then(function (resp){
        console.log(resp);
        _this.tableData = resp.data;
      })
    }
  },
  created() {
    const _this = this;
    axios.get('http://localhost:8000/std/findAll/1/5').then(function (resp){
      console.log(resp);
      _this.tableData = resp.data;
    })
    _this.tableData=[{sid:'123',name:'张三',college:'华侨大学',username:'小白兔白又白'}]
    axios.get('http://localhost:8000/std/findNum').then(function (resp){
      _this.total = resp.data;
    })
  },
  data() {
    return {
      total: null,
      tableData: null,
      value: true,
    }
  }
}
</script>
